import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import login from '@/components/login'
import userinfo from '@/components/userinfo'
import commodityDetails from '@/components/commodityDetails'
import register from '@/components/register'
import product from '@/components/product'
import order from '@/components/order'
import shoppingcar from '@/components/shoppingcar'
import pay from '@/components/pay'
import backstage from '@/components/backstage'
import sku from '@/components/sku'


// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/',
        name: 'HelloWorld',
        component: HelloWorld,
    },
    {
        path: '/commodityDetails',
        name: 'commodityDetails',
        component: commodityDetails,
    },
    {
        path: '/userinfo',
        name: 'userinfo',
        component: userinfo,
    },
    {
        path: '/register',
        name: 'register',
        component: register,
    },
    {
        path: '/product',
        name: 'product',
        component: product,
    },
    {
        path: '/order',
        name: 'order',
        component: order,
    },
    {
        path: '/shoppingcar',
        name: 'shoppingcar',
        component: shoppingcar,
    },
    {
        path: '/pay',
        name: 'pay',
        component: pay,
    },
    {
        path: '/backstage',
        name: 'backstage',
        component: backstage,
    },
    {
        path: '/sku',
        name: 'sku',
        component: sku,
    },
]

const router = new Router({
    routes
})

// // 挂载路由导航守卫
// router.beforeEach((to, from, next) => {
//   // to 将要访问的路径
//   // from 代表从哪个路径跳转而来
//   // next 是一个函数，表示放行
//   //     next()  放行    next('/login')  强制跳转
// //   const token = sessionStorage.getItem('token')
// //   const nextRoute = ["/","commodityDetails","product"];
// //   if (nextRoute.indexOf(to.path) == "-1" ) {
// //     if (token == 1) {
// //         next();
// //     }else{
// //         next('/login')
// //     }
// //   }else{
// //       next();
// //   }
//   next()
// //   if (to.path === '/') return next()
// //   // 获取token
// //   const token = sessionStorage.getItem('token')
// //   if (token != 1) {
// //     next('/')
// //   } else {
// //     next()
// //   }
// })

router.beforeEach((to, from, next) => {
    const nextRoute = ['userinfo', 'order', 'shoppingcar', 'pay', 'backstage', 'sku'] // 需要登录的页面
    let isLogin = sessionStorage.getItem('token') == 1 ? true : false;  // 判断是否登录，本地存储有用户数据则视为已经登录
    // 未登录状态；当路由到 nextRoute 指定页时，跳转至 UserLogIn
    if (nextRoute.indexOf(to.name) >= 0) { // 检测是否登录的页面
        if (sessionStorage.getItem("tokens") == 1) {
            next();
            return
        }
        if (!isLogin) { // 如果未登录（本地存储无用户数据），并且要跳到登录页面
            if (from.name === 'login') {
                next('/')
                return
            }
            // 登录后，跳到到当前页面
            router.push({
                name: 'login',
                params: { redirect: to.fullPath }
            })
        }
    }
    // 已登录状态；当路由到 UserLogIn 时，跳转至 Home
    if (to.name === 'login') {
        // if (isLogin) {
        //     next('/')
        //     return
        // }
        next();
    }
    next() // 必须使用 next ,执行效果依赖 next 方法的调用参数
})
export default router